package com.cqjtu.store.swiper.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.common.Result;
import com.cqjtu.store.swiper.pojo.dto.SwiperDTO;
import com.cqjtu.store.swiper.pojo.dto.SwiperPageRequest;
import com.cqjtu.store.swiper.pojo.vo.SwiperVO;
import com.cqjtu.store.swiper.service.SwiperService;
import com.cqjtu.store.user.pojo.dto.UserRegDTO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: SwiperController
 * @Author: Lucky
 * @Description: 商品控制类
 */
@RestController
@RequestMapping("api/swiper")
public class SwiperController {
    @Autowired
    private SwiperService swiperService;


    @PostMapping("upload")
    public R<String> uploadAvatar(MultipartFile file) {
        System.out.println(file);
        return swiperService.upload(file);
    }

    @GetMapping("download/{flag}")
    public void downloadFile(@PathVariable String flag, @RequestParam(required = true) String play, HttpServletResponse response) {
        swiperService.download(flag, play, response);
    }

    @PostMapping("save")//二级
    @ApiOperation("轮播图添加")
    public R<Void> save(@RequestBody SwiperDTO swiperDTO) {
        swiperService.save(swiperDTO);
        return R.ok();
    }

    @GetMapping("/page")
    public Result page(SwiperPageRequest pageRequest) {
        return Result.succuss(swiperService.page(pageRequest));
    }

    @PutMapping("/update")
    public R<Void> update(@RequestBody SwiperDTO swiperDTO){
        System.out.println(swiperDTO);
        swiperService.update(swiperDTO);
        return R.ok();
    }
    @PutMapping("/getById/{id}")
    public R<SwiperVO> getById(@PathVariable("id") Integer id){
        System.out.println(id);
        SwiperVO swiperVO = swiperService.getById(id);
        return R.ok(swiperVO);
    }
    @DeleteMapping("/delete/{id}")
    public R<Void> delete(@PathVariable("id") Integer id){
        System.out.println(id);
        swiperService.delete(id);
        return R.ok();
    }

    @GetMapping("list")
    public R<List<SwiperVO>> getList() throws IOException {
        List<SwiperVO> swiperVOList=swiperService.getList();
        return R.ok(swiperVOList);
    }
    @GetMapping("getSwiperNum")
    public R<Integer> getSwiperNum() throws IOException {
        Integer num=swiperService.getSwiperNum();
        return R.ok(num);
    }
    @PutMapping("modifySwiperNum/{swiperNum1}")
    public R<Void> modifySwiperNum(@PathVariable("swiperNum1") int num) throws IOException {
        swiperService.modifySwiperNum(num);
        return R.ok();
    }
}
